package com.tensquare.recruit.service;

import com.tensquare.recruit.dao.RecruitDao;
import com.tensquare.recruit.pojo.Recruit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Alfred
 * @description TODO
 * @date 2021/4/23 16:36
 */
@Service
public class RecruitService {

    @Autowired
    private RecruitDao recruitDao;
    @Autowired
    private IdWorker idWorker;

    /**
     * fetch data by rule id
     *
     * @description  增加招聘
     * @param
     * @return
     */
    public void add(Recruit recruit){
        recruit.setId(idWorker.nextId()+"");
        recruitDao.save(recruit);
    }

    /**
     * fetch data by rule id
     *
     * @description  招聘全部列表
     * @param
     * @return
     */
    public List<Recruit> findAll(){
        return recruitDao.findAll();
    }

    /**
     * fetch data by rule id
     *
     * @description  根据ID查询招聘
     * @param
     * @return
     */
    public Recruit findById(String id){
        return recruitDao.findById(id).get();
    }

    /**
     * fetch data by rule id
     *
     * @description  修改招聘
     * @param
     * @return
     */
    public void update(Recruit recruit){
        recruitDao.save(recruit);
    }

    /**
     * fetch data by rule id
     *
     * @description  根据ID删除招聘
     * @param
     * @return
     */
    public void deleteById(String id){
        recruitDao.deleteById(id);
    }

    /**
     * fetch data by rule id
     *
     * @description  构建查询条件
     * @param
     * @return
     */
    public Specification<Recruit> createSpecification(Map searchMap){
        return new Specification<Recruit>(){

            @Override
            public Predicate toPredicate(Root<Recruit> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                List<Predicate> predicateList = new ArrayList<>();
                if(searchMap.get("jobname")!=null && !"".equals(searchMap.get("jobname"))){
                    predicateList.add(cb.like(root.get("jobname").as(String.class),"%"+(String) searchMap.get("jobname")+"%"));
                }
                if(searchMap.get("salary")!=null && !"".equals(searchMap.get("salary"))){
                    predicateList.add(cb.equal(root.get("salary").as(String.class),(String) searchMap.get("salary")));
                }
                if(searchMap.get("condition")!=null && !"".equals(searchMap.get("condition"))){
                    predicateList.add(cb.equal(root.get("condition").as(String.class),(String) searchMap.get("condition")));
                }
                if(searchMap.get("education")!=null && !"".equals(searchMap.get("education"))){
                    predicateList.add(cb.equal(root.get("education").as(String.class),(String) searchMap.get("education")));
                }
                if(searchMap.get("type")!=null && !"".equals(searchMap.get("type"))){
                    predicateList.add(cb.equal(root.get("type").as(String.class),(String) searchMap.get("type")));
                }
                if(searchMap.get("address")!=null && !"".equals(searchMap.get("address"))){
                    predicateList.add(cb.equal(root.get("address").as(String.class),(String) searchMap.get("address")));
                }
                if(searchMap.get("eid")!=null && !"".equals(searchMap.get("eid"))){
                    predicateList.add(cb.equal(root.get("eid").as(String.class),(String) searchMap.get("eid")));
                }
                if(searchMap.get("state")!=null && !"".equals(searchMap.get("state"))){
                    predicateList.add(cb.equal(root.get("state").as(String.class),(String) searchMap.get("state")));
                }
                if(searchMap.get("label")!=null && !"".equals(searchMap.get("label"))){
                    predicateList.add(cb.equal(root.get("label").as(String.class),(String) searchMap.get("label")));
                }
                if(searchMap.get("content1")!=null && !"".equals(searchMap.get("content1"))){
                    predicateList.add(cb.equal(root.get("content1").as(String.class),(String) searchMap.get("content1")));
                }

                return cb.and(predicateList.toArray(new Predicate[predicateList.size()]));
            }
        };
    }

    /**
     * fetch data by rule id
     *
     * @description  根据条件查询招聘列表
     * @param
     * @return
     */
    public List<Recruit> findSearch(Map searchMap){
        Specification<Recruit> specification = createSpecification(searchMap);
        return recruitDao.findAll(specification);
    }

    /**
     * fetch data by rule id
     *
     * @description  分页 + 条件查询
     * @param
     * @return
     */
    public Page<Recruit> findSearch(Map searchMap, int page, int size){
        Specification<Recruit> specification = createSpecification(searchMap);
        PageRequest pageRequest = PageRequest.of(page-1,size);
        return recruitDao.findAll(specification,pageRequest);
    }

    /**
     * fetch data by rule id
     *
     * @description  查询最新职位列表(按创建日期降序排序)
     * @param
     * @return
     */
    public List<Recruit> findTop4ByStateOrderByCreatetimeDesc(String state){
        return recruitDao.findTop4ByStateOrderByCreatetimeDesc(state);
    }

    /**
     * fetch data by rule id
     *
     * @description  最新职位列表
     * @param
     * @return
     */
    public List<Recruit> newList(){
        return recruitDao.findTop12ByStateNotOrderByCreatetimeDesc("0");
    }
}
